﻿using HCQ2_Common.SQL;
using HCQ2_IDAL;
using HCQ2_Model.VRDD.InputDto._211026;
using HCQ2_Model.VRDD.OutputDto._211026;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HCQ2_DAL_MSSQL
{
    public partial class T_VRPlayListDAL: IT_VRPlayListDAL
    {
        /// <summary>
        ///  获取点播列表
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public List<OutputPlayTable> GetPlayTable(QueryPlayTable param)
        {
            sb?.Clear();
            sb.Append($@"SELECT TOP {param.rows} * FROM (SELECT id,OrderCode,CommodityName=(SELECT file_name FROM dbo.T_DocumentInfo 
            WHERE file_id=p.CommodityID),ROW_NUMBER() OVER(ORDER BY CreateDate {(param.SortType>0?"ASC": "DESC")}) AS num,
             StoreName=(SELECT folder_name FROM dbo.T_DocumentFolder WHERE folder_id IN(
            SELECT store_id FROM dbo.T_DocumentInfo WHERE file_id=p.CommodityID)),user_name AS UserName,
            Content,PlayStatus,CONVERT(varchar(100),CreateDate,120)AS CreateDate,
            VideoUrl=(SELECT vUrl FROM dbo.T_DocumentInfo WHERE file_id=p.CommodityID)FROM T_VRPlayList p WHERE 1=1 ");
            if (!string.IsNullOrEmpty(param.DeviceCode))
                sb.Append($" AND DeviceCode='{param.DeviceCode}' ");
            if (!string.IsNullOrEmpty(param.userid))
                sb.Append($" AND userid='{param.userid}' ");
            if (param.PlayStatus > -1)
                sb.Append($" AND PlayStatus={param.PlayStatus} ");
            sb.Append($")play WHERE play.num>{(param.page - 1) * param.rows};");
            return SqlHelper.ExecuteList<OutputPlayTable>(sb.ToString());
        }
        /// <summary>
        ///  后台初始化点播列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        /// <param name="playStatus"></param>
        /// <param name="userName"></param>
        /// <returns></returns>
        public List<OutputPlayList> GetPlayList(int page, int rows, int playStatus, string userName,out int total)
        {
            sb?.Clear();
            string temp = $"SELECT TOP {rows} * FROM (";
            sb.Append($@"SELECT ROW_NUMBER() OVER(ORDER BY CreateDate DESC)AS num,id,CommodityID,OrderCode,PlayStatus,
            user_name AS UserName,Content,
            picCount=(SELECT COUNT(0) FROM T_VRPlayPictureList WHERE playId=p.id),
            CONVERT(varchar(100),CreateDate,120)AS CreateDate,CONVERT(varchar(100),UpdateDate,120)AS UpdateDate,
            CommodityName=(SELECT file_name FROM dbo.T_DocumentInfo WHERE file_id=p.CommodityID ");
            if (!string.IsNullOrEmpty(userName))
                sb.Append($"AND file_name LIKE '%{userName}%' ");
            sb.Append($@"),
            VideoUrl=(SELECT vUrl FROM dbo.T_DocumentInfo WHERE file_id=p.CommodityID),
            StoreName=(SELECT folder_name FROM dbo.T_DocumentFolder WHERE folder_id IN(
            SELECT store_id FROM dbo.T_DocumentInfo WHERE file_id=p.CommodityID)) FROM T_VRPlayList p
            WHERE 1=1 ");
            if (!string.IsNullOrEmpty(userName))
                sb.Append($" AND (user_name LIKE '%{userName}%' OR OrderCode LIKE '%{userName}%') ");
            total = SqlHelper.ExecuteScalarInt32("SELECT COUNT(0) AS num FROM (" + sb.ToString() + ")play ");
            return SqlHelper.ExecuteList<OutputPlayList>(temp + sb.ToString() + $")play WHERE play.num>{(page - 1) * rows}");
        }
    }
}